﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace Chizx.Web.apply
{
    public partial class Add : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (null == Session["UserEntity"])
            {
                Response.Redirect("/e/Login.aspx");
            }
            if (Session["UserDegree"].ToString() == "SS")
            {
                Chizx.Model.UserModel model = (Chizx.Model.UserModel)Session["UserEntity"];
                this.txtzu_id.Text = model.zu_id;
                this.txtzu_id.Enabled = false;
            }
        }

        		protected void btnSave_Click(object sender, EventArgs e)
		{

            string uid_this = this.txtzu_id.Text;
            string cid_this = "0";
            string cname_this = this.txtza_cname.Text;
            string caddress_this = this.txtza_caddress.Text;
			string strErr="";
            if (this.txtzsm_type.SelectedItem.Value != "s0") 
            {//分配实习
                cid_this = this.txtza_cid.Text;
                Chizx.BLL.CompanysBll cb = new Chizx.BLL.CompanysBll();
                Chizx.Model.CompanysModel cm = new Chizx.Model.CompanysModel();
                cm = cb.GetModel(int.Parse(cid_this));
                if (cm.zcom_status == "c1")
                {//已结束
                    strErr += "对不起！该单位招聘人数已满！\\n";
                }
                else
                {
                    cm.zcom_applynum += 1;
                    cb.Update(cm);
                }
                cname_this = cm.zcom_name;
                caddress_this = cm.zcom_address;
            }
            
            if (uid_this.Trim().Length == 0)
			{
				strErr+="zu_id不能为空！\\n";	
			}
            if (!PageValidate.IsNumber(cid_this))
			{
                if (cid_this != "0")
				strErr+="za_cid格式错误！\\n";	
			}
            if (cname_this.Trim().Length == 0)
			{
				strErr+="za_cname不能为空！\\n";	
			}
            if (caddress_this.Trim().Length == 0)
			{
				strErr+="za_caddress不能为空！\\n";	
			}
			if(this.txtza_desc.Text.Trim().Length==0)
			{
				strErr+="za_desc不能为空！\\n";	
			}
			

			if(strErr!="")
			{
				MessageBox.Show(this,strErr);
				return;
			}
            string zu_id = uid_this;
            int za_cid = int.Parse(cid_this);
            string za_cname = cname_this;
            string za_caddress = caddress_this;
			string za_desc=this.txtza_desc.Text;
			

			Chizx.Model.ApplyModel model=new Chizx.Model.ApplyModel();
			model.zu_id=zu_id;
			model.za_cid=za_cid;
			model.za_cname=za_cname;
			model.za_caddress=za_caddress;
			model.za_desc=za_desc;
            model.za_createdate = Chizx.Common.DateHelper.getUtcNow();
			model.za_status="a0";

            //写入系统日志
            if (Session["UserEntity"] == null)
            {
                Response.Redirect("/e/Login.aspx");
            }
            else
            {
                
                    
                  
                Chizx.BLL.ApplyBll bll = new Chizx.BLL.ApplyBll();
                bll.Add(model);
                Chizx.Model.UserModel sbmmodel = (Chizx.Model.UserModel)Session["UserEntity"];
                Chizx.BLL.SystemLogBll sbll = new Chizx.BLL.SystemLogBll();
                Chizx.Model.SystemLogModel slm = new Chizx.Model.SystemLogModel();
                slm.zu_id = sbmmodel.zu_id;
                slm.zl_dowhen = DateTime.Now;
                slm.zl_dowhat = "提交实习申请";
                slm.zl_dotype = "3";//0:Test,1:Auth,2:Read,3:Write,4:Del,5:Inpor
                slm.zl_dodesc = "操作成功";
                sbll.Add(slm);
                Chizx.Model.MessageModel mm = new Chizx.Model.MessageModel();
                Chizx.BLL.MessageBll mb = new Chizx.BLL.MessageBll();
                mm.zu_id = model.zu_id;
                mm.zmsg_topic = "实习申请投递成功";
                mm.zmsg_context = "尊敬的用户 " + sbmmodel.zu_id + " ,您的实习申请已经投递成功！请继续留言平台的第一消息！";
                mm.zmsg_createdate = DateTime.Now;
                mm.zmsg_status = "m0";
                mb.Add(mm);
                if (Session["UserDegree"].ToString() == "SS")
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "/e/index.aspx");
                }
                else
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "list.aspx");
                }
               
            }

		}


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}
